/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.cggveritas.crewintranet.jms.dao.jpacontroller;

import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.NonexistentEntityException;
import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.PreexistingEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import fr.cggveritas.crewintranet.jms.entities.CvMission;
import fr.cggveritas.crewintranet.jms.entities.CvEquipedepartement;
import fr.cggveritas.crewintranet.jms.entities.CvListedepartement;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Alassane
 */
public class CvListedepartementJpaController extends DaoImp implements Serializable {

    public CvListedepartementJpaController() {
        super();
    }

    public void create(CvListedepartement cvListedepartement) throws PreexistingEntityException, Exception {
        if (cvListedepartement.getCvEquipedepartementList() == null) {
            cvListedepartement.setCvEquipedepartementList(new ArrayList<CvEquipedepartement>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvMission idmission = cvListedepartement.getIdmission();
            if (idmission != null) {
                idmission = em.getReference(idmission.getClass(), idmission.getIdmission());
                cvListedepartement.setIdmission(idmission);
            }
            List<CvEquipedepartement> attachedCvEquipedepartementList = new ArrayList<CvEquipedepartement>();
            for (CvEquipedepartement cvEquipedepartementListCvEquipedepartementToAttach : cvListedepartement.getCvEquipedepartementList()) {
                cvEquipedepartementListCvEquipedepartementToAttach = em.getReference(cvEquipedepartementListCvEquipedepartementToAttach.getClass(), cvEquipedepartementListCvEquipedepartementToAttach.getIdequipedepartement());
                attachedCvEquipedepartementList.add(cvEquipedepartementListCvEquipedepartementToAttach);
            }
            cvListedepartement.setCvEquipedepartementList(attachedCvEquipedepartementList);
            em.persist(cvListedepartement);
            if (idmission != null) {
                idmission.getCvListedepartementList().add(cvListedepartement);
                idmission = em.merge(idmission);
            }
            for (CvEquipedepartement cvEquipedepartementListCvEquipedepartement : cvListedepartement.getCvEquipedepartementList()) {
                CvListedepartement oldIddepartementOfCvEquipedepartementListCvEquipedepartement = cvEquipedepartementListCvEquipedepartement.getIddepartement();
                cvEquipedepartementListCvEquipedepartement.setIddepartement(cvListedepartement);
                cvEquipedepartementListCvEquipedepartement = em.merge(cvEquipedepartementListCvEquipedepartement);
                if (oldIddepartementOfCvEquipedepartementListCvEquipedepartement != null) {
                    oldIddepartementOfCvEquipedepartementListCvEquipedepartement.getCvEquipedepartementList().remove(cvEquipedepartementListCvEquipedepartement);
                    oldIddepartementOfCvEquipedepartementListCvEquipedepartement = em.merge(oldIddepartementOfCvEquipedepartementListCvEquipedepartement);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findCvListedepartement(cvListedepartement.getIddepartement()) != null) {
                throw new PreexistingEntityException("CvListedepartement " + cvListedepartement + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(CvListedepartement cvListedepartement) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvListedepartement persistentCvListedepartement = em.find(CvListedepartement.class, cvListedepartement.getIddepartement());
            CvMission idmissionOld = persistentCvListedepartement.getIdmission();
            CvMission idmissionNew = cvListedepartement.getIdmission();
            List<CvEquipedepartement> cvEquipedepartementListOld = persistentCvListedepartement.getCvEquipedepartementList();
            List<CvEquipedepartement> cvEquipedepartementListNew = cvListedepartement.getCvEquipedepartementList();
            if (idmissionNew != null) {
                idmissionNew = em.getReference(idmissionNew.getClass(), idmissionNew.getIdmission());
                cvListedepartement.setIdmission(idmissionNew);
            }
            List<CvEquipedepartement> attachedCvEquipedepartementListNew = new ArrayList<CvEquipedepartement>();
            for (CvEquipedepartement cvEquipedepartementListNewCvEquipedepartementToAttach : cvEquipedepartementListNew) {
                cvEquipedepartementListNewCvEquipedepartementToAttach = em.getReference(cvEquipedepartementListNewCvEquipedepartementToAttach.getClass(), cvEquipedepartementListNewCvEquipedepartementToAttach.getIdequipedepartement());
                attachedCvEquipedepartementListNew.add(cvEquipedepartementListNewCvEquipedepartementToAttach);
            }
            cvEquipedepartementListNew = attachedCvEquipedepartementListNew;
            cvListedepartement.setCvEquipedepartementList(cvEquipedepartementListNew);
            cvListedepartement = em.merge(cvListedepartement);
            if (idmissionOld != null && !idmissionOld.equals(idmissionNew)) {
                idmissionOld.getCvListedepartementList().remove(cvListedepartement);
                idmissionOld = em.merge(idmissionOld);
            }
            if (idmissionNew != null && !idmissionNew.equals(idmissionOld)) {
                idmissionNew.getCvListedepartementList().add(cvListedepartement);
                idmissionNew = em.merge(idmissionNew);
            }
            for (CvEquipedepartement cvEquipedepartementListOldCvEquipedepartement : cvEquipedepartementListOld) {
                if (!cvEquipedepartementListNew.contains(cvEquipedepartementListOldCvEquipedepartement)) {
                    cvEquipedepartementListOldCvEquipedepartement.setIddepartement(null);
                    cvEquipedepartementListOldCvEquipedepartement = em.merge(cvEquipedepartementListOldCvEquipedepartement);
                }
            }
            for (CvEquipedepartement cvEquipedepartementListNewCvEquipedepartement : cvEquipedepartementListNew) {
                if (!cvEquipedepartementListOld.contains(cvEquipedepartementListNewCvEquipedepartement)) {
                    CvListedepartement oldIddepartementOfCvEquipedepartementListNewCvEquipedepartement = cvEquipedepartementListNewCvEquipedepartement.getIddepartement();
                    cvEquipedepartementListNewCvEquipedepartement.setIddepartement(cvListedepartement);
                    cvEquipedepartementListNewCvEquipedepartement = em.merge(cvEquipedepartementListNewCvEquipedepartement);
                    if (oldIddepartementOfCvEquipedepartementListNewCvEquipedepartement != null && !oldIddepartementOfCvEquipedepartementListNewCvEquipedepartement.equals(cvListedepartement)) {
                        oldIddepartementOfCvEquipedepartementListNewCvEquipedepartement.getCvEquipedepartementList().remove(cvEquipedepartementListNewCvEquipedepartement);
                        oldIddepartementOfCvEquipedepartementListNewCvEquipedepartement = em.merge(oldIddepartementOfCvEquipedepartementListNewCvEquipedepartement);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = cvListedepartement.getIddepartement();
                if (findCvListedepartement(id) == null) {
                    throw new NonexistentEntityException("The cvListedepartement with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvListedepartement cvListedepartement;
            try {
                cvListedepartement = em.getReference(CvListedepartement.class, id);
                cvListedepartement.getIddepartement();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The cvListedepartement with id " + id + " no longer exists.", enfe);
            }
            CvMission idmission = cvListedepartement.getIdmission();
            if (idmission != null) {
                idmission.getCvListedepartementList().remove(cvListedepartement);
                idmission = em.merge(idmission);
            }
            List<CvEquipedepartement> cvEquipedepartementList = cvListedepartement.getCvEquipedepartementList();
            for (CvEquipedepartement cvEquipedepartementListCvEquipedepartement : cvEquipedepartementList) {
                cvEquipedepartementListCvEquipedepartement.setIddepartement(null);
                cvEquipedepartementListCvEquipedepartement = em.merge(cvEquipedepartementListCvEquipedepartement);
            }
            em.remove(cvListedepartement);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<CvListedepartement> findCvListedepartementEntities() {
        return findCvListedepartementEntities(true, -1, -1);
    }

    public List<CvListedepartement> findCvListedepartementEntities(int maxResults, int firstResult) {
        return findCvListedepartementEntities(false, maxResults, firstResult);
    }

    private List<CvListedepartement> findCvListedepartementEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(CvListedepartement.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public CvListedepartement findCvListedepartement(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(CvListedepartement.class, id);
        } finally {
            em.close();
        }
    }

    public int getCvListedepartementCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<CvListedepartement> rt = cq.from(CvListedepartement.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
    public List<CvListedepartement> findByMission(CvMission cvMission){
        EntityManager em = getEntityManager();
        try {
           String queryString = "select c from CvListedepartement c "
                    + "where c.idmission = :x";
            Query query = em.createQuery(queryString);
            query.setParameter("x", cvMission); 
            
            return query.getResultList();
        } finally {
            em.close();
        }
    }
    
}
